package cn.tedu.springmvc2.controller;

import cn.tedu.springmvc2.mapper.UserMapper;
import cn.tedu.springmvc2.pojo.dto.UserDTO;
import cn.tedu.springmvc2.pojo.dto.UserDTOVO1;
import cn.tedu.springmvc2.pojo.entity.User;
import cn.tedu.springmvc2.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

//@Controller
@RestController//加上这个注解下面就不用单独加@ResponseBody了
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @RequestMapping(value = "/users/reg",method = RequestMethod.POST)
    //@ResponseBody
    public String reg(UserDTO userDTO){
        System.out.println(userDTO);
        User user=new User();
        //user.setUsername(userDTO.getUsername());
        //user.setPassword(userDTO.getPassword());
       // user.setNickname(userDTO.getNickname());
        //等同于下面的
        BeanUtils.copyProperties(userDTO,user);
        user.setCreated(new Date());
        int num=userMapper.insertUser(user);
        return num>0?"注册成功":"注册失败";
    }
    @RequestMapping(value = "/users/reg",method = RequestMethod.GET)
    public String insert(){
        System.out.println("!!!!!");
    return "耶耶耶";
    }
    @RequestMapping("/users/delete")
   // @ResponseBody
    public String delete(int id){
        int num=userMapper.deleteUser(id);
        return num>0?"删除成功":"删除失败";
    }
    @RequestMapping("/users/update")
    //@ResponseBody
    public String update(UserDTO userDTO){
        User user=new User();
        BeanUtils.copyProperties(userDTO,user);
        int num=userMapper.updateUser(user);
        return num>0?"修改成功":"修改失败";
    }
    @RequestMapping("/user/select")
    public List<UserVO> select(){
        return userMapper.findAll();
    }
}
